<template>
  <div>
    <el-form :model="form" :rules="rules" ref="form" label-width="130px" class="demo-ruleForm" size="mini">

      <el-form-item label="类别" prop="type" >
        <el-input v-model="form.type==1 ? '学生':'老师'" readonly clearable></el-input>
      </el-form-item>

      <el-form-item label="刷脸次数" prop="limitCount">
        <el-input v-model="form.limitCount" placeholder="请输入刷脸次数" type="number" clearable></el-input>
      </el-form-item>

      <el-form-item label="单次限制金额" prop="limitAmt">
        <el-input v-model="form.limitAmt" placeholder="请输入限制金额" type="number" clearable></el-input>
      </el-form-item>

      <el-form-item label="刷脸间隔" prop="limitTime">
        <el-input v-model="form.limitTime" placeholder="请输入刷脸间隔"  type="number" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">{{$t("action.save")}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Cookies from "js-cookie"

export default {
  name: "edit",
  data() {
    return {
      url: this.$Config.url,
      id: '',
      form: {
      },
      rules: {
        limitTime: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        limitAmt: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        limitCount: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
      }
    };
  },
  mounted() {
  },
  methods: {
    resetData(id = 1) {
      this.$refs["form"].resetFields();//先清空
      const that = this;
      if (id) {
        this.id = id;
      }
      this.$axios.get(that.url + '/admin/payConfig/get/{id}', {
        params: {
          id,
        },
        headers: {
          "x-baie-admin-token": Cookies.get("token")
        }
      })
        .then(function (res) {
          if (res.status == 200) {
            let data = res.data.data;
            that.form = data
            that.id = data.id;
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }

        })
        .catch(function (error) {
          console.log(error);
        });
    },

    submitForm(formName) {
      const that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.send();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    send() {
      const that = this;
      if(that.form.limitTime<=0){
        that.$message({
          message: '时间间隔大于0',
          type: "warning"
        });
        return false;
      }
      if(that.form.limitAmt<0){
        that.$message({
          message: '限制金额不能为负数',
          type: "warning"
        });
        return false;
      }
      if(that.form.limitCount < 0){
        that.$message({
          message: '刷脸次数不能为负数',
          type: "warning"
        });
        return false;
      }
      const data = that.form;
      this.$axios.put(that.url + '/admin/payConfig/update', that.$qs.stringify(data), {
        headers: {
          "x-baie-admin-token": Cookies.get("token")
        }
      }).then(function (res) {
        if (res.status == 200) {
          if(res.data.code==200){
            if (that.id == "") {
              that.$message({
                message: "添加成功",
                type: "success"
              });
            } else {
              that.$message({
                message:"修改成功",
                type: "success"
              });
            }
            that.$emit("hideEdit", false);
          }else{
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        } else {
          that.$message({
            message: res.statusText,
            type: 'warning'
          });
        }

      })
        .catch(function (error) {
          console.log(error);
        });
    },
  }
}
</script>

<style scoped lang="scss">
.el-select, .el-date-editor {
  width: 30%;
}

.el-select:not(:last-child) {
  margin-right: 3%;
  display: inline-block;
}
</style>
